<?php

namespace App\Admin\Exporter;

use Biin2013\ImportExport\Export;
use Dcat\Admin\Grid\Exporters\AbstractExporter;
use Illuminate\Support\Collection;

abstract class BaseExporter extends AbstractExporter
{
    protected string $permission;
    protected string $type = '';
    protected int $maxSize = 10000;
    protected Export $export;

    abstract protected function typeName(): string;

    abstract protected function title(): array;

    abstract protected function data(Collection $list): array;

    public function export()
    {
        $list = $this->buildData(1, $this->maxSize);
        $this->export = new Export();
        $this->export->setTitle($this->title())->setData($this->data($list))->build();

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename=' . $this->typeName() . '-' . date('YmdHis') . '.xlsx');
        header('Cache-Control: max-age=0');
        $this->export->getWriter()->save('php://output');
        die;
    }


    protected function mergeRow(int $worksheetIndex, $startColumn, $endColumn, $startRow, $row)
    {
        while ($startColumn <= $endColumn) {
            $i = 0;
            while ($i < $row) {
                $this->export->addMergeCells(
                    $worksheetIndex,
                    $startColumn,
                    $startRow,
                    $startColumn,
                    $startRow + $row
                );
                $merge[] = [];
                $i++;
            }
            $startColumn++;
        }
    }
}
